package leetcode.leetcode_2273;

import java.util.HashMap;
import java.util.List;

class Solution {
    public List<String> removeAnagrams(String[] words) {
        int length = words.length;
        int left =0;
        int right =0;
        while(right<length){
            if (!isEqual(words[left],words[right])){
                left++;
                words[left]=words[right];
            }
            right++;
        }
        return List.of(words).subList(0,left+1);
    }
    public boolean isEqual(String a, String b){
        if (a.length()!=b.length()){
            return false;
        }
        HashMap<Character, Integer> characterA = new HashMap<>();
        HashMap<Character, Integer> characterB = new HashMap<>();
        for (int i = 0; i < a.length(); i++) {
            characterA.put(a.charAt(i),characterA.getOrDefault(a.charAt(i),0)+1);
            characterB.put(b.charAt(i),characterB.getOrDefault(b.charAt(i),0)+1);
        }
        for (Character character : characterA.keySet()) {
            if (!characterA.get(character).equals(characterB.get(character))){
                return false;
            }
        }
        return true;
    }
}